package com.longrise.android.byjk.utils;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.common.UrlConstants;
import com.longrise.android.byjk.model.UserInfor;
import com.longrise.common.datasource.remote.LoadDataManager;
import com.longrise.common.utils.AppUtil;

/**
 * Created by callxkj on 2017/10/26.
 * 获取url地址的帮助类
 */

public class GetWebUrlHelper {
    public static final String KHLX = "khlx"; //课后练习
    public static final String ZSK = "zsk";   //知识库
    public static final String MRYL = "mryl"; //每日一练 题库
    public static final String ZYSX = "fpzysx"; //发票注意事项
    public static final String ZCXY = "zcxy"; //注册协议
    public static final String GRZY = "grzy"; //个人主页, 展业秀页面
    public static final String GMXY = "gmxy"; //购买协议
    public static final String XXXY = "xxxy"; //学习协议
    public static final String HRA = "hra"; //hra首页banner点击跳转

    public static void getWebUrl(final String urltype, final OnGetWebUrlResultListener listener) {
        final EntityBean bean = new EntityBean();
        bean.set("devicetype", "android");
        bean.set("urltype", urltype);
        if (GRZY.equals(urltype)) {
            bean.set("precardno", UserInfor.getInstance().getUsersfzh());
            bean.set("userid", UserInfor.getInstance().getUserid());
        }
        EntityBean fbean = new EntityBean();
        fbean.set("bean", bean);
        LoadDataManager.getInstance().callService(null, UrlConstants.BaseUrl, "bbt_getAppEntryPath",
                fbean, new LoadDataManager.OnRequestCallListener() {
                    @Override
                    public void onSuccess(String key, String service, Object result) {
                        try {
                            if (result != null) {
                                EntityBean bean = (EntityBean) result;
                                EntityBean resultBean = bean.getBean("result");
                                int resultstate = (Integer) bean.get("resultstate");
                                String resultdesc = bean.getString("resultdesc");
                                if (resultstate == 1) { //pageurl
                                    String pageurl = resultBean.getString("pageurl");
                                    parseUrl(urltype, pageurl, listener);
                                } else {
                                    listener.onError(resultdesc);
                                }
                            } else {
                                listener.onError("返回结果为空");
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                            listener.onError("解析返回结果失败");
                        }
                    }

                    @Override
                    public void onError(String key, String service, Throwable ex, boolean isOnCallback) {
                        listener.onError(AppUtil.getString(R.string.networkerror));
                    }

                    @Override
                    public void onFinished(String key, String service) {

                    }
                });
    }


    private static void parseUrl(String urltype, String url, OnGetWebUrlResultListener listener) {
        String carno = UserInfor.getInstance().getUsersfzh();
        String userid = UserInfor.getInstance().getUserid();
        int screenWidth = AppUtil.getScreenWidth();
        int screenWidthdp = AppUtil.px2dip(screenWidth);

        switch (urltype) {
            case KHLX: //课后练习
                url = url + "?cardno=" + carno + "&uid=" + userid + "&screenwidth=" + screenWidthdp;
                break;
            case MRYL: //每日一练
                url = url + "?cardno=" + carno + "&userid=" + userid;
                break;
            case ZYSX: //发票注意事项
                url = url + "?screenwidth=" + screenWidthdp;
                break;
            case GRZY: //个人主页 展业秀
                url = url + "?cardno=" + carno + "&userid=" + userid + "&screenwidth=" + screenWidthdp;
                break;
            case HRA:  //hra首页banner点击跳转
                url = url + "?screenwidth=" + screenWidthdp;
                break;
            default:
                break;
        }

        listener.onSuccess(url);
    }


    public interface OnGetWebUrlResultListener {
        void onSuccess(String url);

        void onError(String errormsg);
    }
}
